#include <stdio.h>
#include <sys/types.h>
#include <string.h>



int main(int argc, char *argv[])
{
    //
    key_t key = ftok("root", 1);
    int msqid = msgget(key, IPC_CREAT | 0664);

    // struct msgbuf
    // {
    //     long mtype;        //消息类型
    //     char mtext[1024];     //消息正文
    // } msg;

    struct msgbuf msg;
    msg.mtype=1;
    strcpy(msg.mtext, "hello world");
    

    int ret=msgsnd(msqid, &msg, 1024, 0);
    ret=msgsnd(msqid, &msg, 1024, 0);
    ret=msgsnd(msqid, &msg, 1024, 0);

    struct msgbuf buf;
    ssize_t cnt=msgrcv(msqid, &buf, 1024, 1, 0);
    printf("111: cnt=[%ld], buf=[%s]\n", cnt, buf.mtext);
    cnt=msgrcv(msqid, &buf, 1024, 1, 0);
    printf("222: cnt=[%ld], buf=[%s]\n", cnt, buf.mtext);
    cnt=msgrcv(msqid, &buf, 1024, 1, 0);
    printf("333: cnt=[%ld], buf=[%s]\n", cnt, buf.mtext);
    cnt=msgrcv(msqid, &buf, 1024, 1, 0);
    printf("444: cnt=[%ld], buf=[%s]\n", cnt, buf.mtext);

    return 0;
}